﻿/*++

Copyright (c) 2011 Rainr Corporation

Module Name:

	 Rainr.Utilities.Common.RegexUtility

Abstract:

	借助于系统正则验证匹配查询部分文本数据

Author:

	Zhou Junyu (Rainr)    2011-01-12

--*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Rainr.Utilities.Common
{
    /// <summary>
    /// 正则表达式工具类
    /// </summary>
    public class RegexUtility
    {
        #region 实例属性
        private static RegexUtility regexs;
        public static RegexUtility CurrentRegex
        {
            get
            {
                if (null == regexs) regexs = new RegexUtility();
                return regexs;
            }
        }
        public string IsTime = @"^([1-9]\d{3}-((0?[1-9])|(1[0-2]))-((0?[1-9])|([1-2]\d)|(3[0-1])))|([1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|(3[0-1])))$";
        public string IsTime_ShortDate = @"^[1-9]\d{3}-((0?[1-9])|(1[0-2]))-((0?[1-9])|([1-2]\d)|(3[0-1]))$";
        public string IsTime_ShortNumber = @"^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|(3[0-1]))$";
        public string IsIDCard_CN = @"^([1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|(3[0-1]))\d{3}(\d|X))|([1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|(3[0-1]))\d{3})$";
        public string IsEmail = @"^([\w\.-]+)@([\w\.-]+)\.(a-zA-Z\.){2,6}$";
        public string IsURL = @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
        public string IsZipCode_CN = @"^\d{6}$";
        public string IsLanguage_CN = @"^[\u4E00-\u9FFF]+$";
        public string IsLanguage_CN_KR_JP = @"^[\u2E80-\u9FFF]+$";
        public string IsValidate_Number = @"^\d+$";
        public string IsValidate_Letter = @"^[A-Za-z]+$";
        public string IsValidate_NumberAndLetter = @"^[A-Za-z0-9]+$";
        #endregion

        #region 实例方法
        /// <summary>
        /// 判断日期格式
        /// 19900201或1990-02-01或1990-2-1
        /// </summary>
        /// <param name="input">与匹配的值</param>
        /// <returns></returns>
        public bool MatchIsTime(string input)
        {
            return IsMatch(input, this.IsTime);
        }
        /// <summary>
        /// 判断短日期格式
        /// 1990-02-01或1990-2-1
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsTime_ShortDate(string input)
        {
            return IsMatch(input, this.IsTime_ShortDate);
        }
        /// <summary>
        /// 判断短数字日期格式(8位)
        /// 19900201
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsTime_ShortNumber(string input)
        {
            return IsMatch(input, this.IsTime_ShortNumber);
        }
        /// <summary>
        /// 判断身份证号码
        /// 中国15或18位
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsIDCard_CN(string input)
        {
            return IsMatch(input, this.IsIDCard_CN);
        }
        /// <summary>
        /// 判断Email地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsEmail(string input)
        {
            return IsMatch(input, this.IsEmail);
        }
        /// <summary>
        /// 判断是否为URL
        /// </summary>
        /// <param name="intpu"></param>
        /// <returns></returns>
        public bool MatchIsURL(string input)
        {
            return IsMatch(input, this.IsURL);
        }
        /// <summary>
        /// 判断邮编号码(中国)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsZipCode_CN(string input)
        {
            return IsMatch(input, this.IsZipCode_CN);
        }
        /// <summary>
        /// 判断中文语言
        /// 简体或繁体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsLanguage_CN(string input)
        {
            return IsMatch(input, this.IsLanguage_CN);
        }
        /// <summary>
        /// 判断数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsValidate_Number(string input)
        {
            return IsMatch(input, this.IsValidate_Number);
        }
        /// <summary>
        /// 判断字母
        /// 大写或小写
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsValidate_Letter(string input)
        {
            return IsMatch(input, this.IsValidate_Letter);
        }
        /// <summary>
        /// 判断字母或者数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool MatchIsValidate_NumberAndLetter(string input)
        {
            return IsMatch(input, this.IsValidate_NumberAndLetter);
        }
        /// <summary>
        /// 判断是否包含BASE64字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool MatchIsBase64String(string input)
        {
            //A-Z, a-z, 0-9, +, /, =
            return IsMatch(input, @"[A-Za-z0-9\+\/\=]");
        }
        /// <summary>
        /// 判断是否包含SQL危险字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool MatchIsSafeSqlString(string input)
        {
            return IsMatch(input, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }
        #endregion

        #region 静态方法
        public static bool IsMatch(string input, string pattern)
        {
            if (input == null || pattern == null) return false;
            return Regex.IsMatch(input, pattern);
        }
        public static string[] MatcheValues(string input, string pattern)
        {
            if (input == null || pattern == null) return null;
            string[] values = null;
            MatchCollection mc = Regex.Matches(input, pattern);
            if (mc != null && mc.Count > 0)
            {
                values = new string[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    values[i] = mc[i].Value;
                }
            }
            return values;
        }
        public static int[] MatcheIndexs(string input, string pattern)
        {
            if (input == null || pattern == null) return null;
            int[] indexs = null;
            MatchCollection mc = Regex.Matches(input, pattern);
            if (mc != null && mc.Count > 0)
            {
                indexs = new int[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    indexs[i] = mc[i].Index;
                }
            }
            return indexs;
        }
        public static MatchCollection Matches(string input, string pattern)
        {
            if (input == null || pattern == null) return null;
            return Regex.Matches(input, pattern);
        }
        public static string Replace(string input, string pattern, string replacement)
        {
            if (input == null || pattern == null || replacement == null) return null;
            return Regex.Replace(input, pattern, replacement);
        }
        #endregion
    }
}
